/**
 * Get the commit message by user options
 * @param {object} options `object`
 * @returns commit message `string`
 */
function createCommitMessage(options) {
  return createMessage(
    createType(options.type),
    createScope(options.scope),
    createTitle(options.title),
    createDescription(options.description),
    createIssue(options.issue),
    createBreakingChange(options.breaking)
  );
}

function createType(answer) {
  return answer ? answer : '';
}

function createScope(answer) {
  return answer ? `(${answer})` : '';
}

function createTitle(answer) {
  return answer ? `: ${answer}\n` : '';
}

function createDescription(answer) {
  return answer ? `\n${answer}\n` : '';
}

function createIssue(answer) {
  return answer ? answer
    .split(/[\D]+/g)
    .filter(issue => !!issue)
    .map(issue => `#${issue}, `)
    .join('')
    .replace(/^#/, '\nCloses: #')
    .replace(/, $/, '\n')
    : '';
}

function createBreakingChange(answer) {
  return answer ? `\nBREAKING CHANGE: ${answer}\n` : '';
}

function createMessage(...answers) {
  return answers.filter(answer => answer !== '').join('');
}

module.exports = createCommitMessage;
